package com.lanchetech.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 商家
 */
@ApiModel(value = "com-lanchetech-entity-Merchant")
@Data
@Builder
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class Merchant implements UserDetails, Serializable {
    /**
     * id
     */
    @ApiModelProperty(value = "id")
    private Long id;

    /**
     * 商家角色id
     */
    @ApiModelProperty(value = "商家角色id")
    private Long roleId;

    /**
     * 商家账号
     */
    @ApiModelProperty(value = "商家账号")
    private String username;

    /**
     * 密码
     */
    @ApiModelProperty(value = "密码")
    private String password;

    /**
     * 昵称
     */
    @ApiModelProperty(value = "昵称")
    private String nickname;

    /**
     * 头像
     */
    @ApiModelProperty(value = "头像")
    private String avatar;

    /**
     * 最近一次修改密码时间
     */
    @ApiModelProperty(value = "最近一次修改密码时间")
    private Date lastPasswordResetDate;

    /**
     * 商家状态 0 未知 1 正常
     */
    @ApiModelProperty(value = "商家状态 0 未知 1 正常")
    private Integer status;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    private Date createdAt;

    /**
     * 更新时间
     */
    @ApiModelProperty(value = "更新时间")
    private Date updatedAt;

    /**
     * 租户id
     */
    @ApiModelProperty(value = "租户id")
    private Long tenantId;

    /**
     * 注册商家名称
     */
    @ApiModelProperty(value = "注册商家名称")
    private String registerName;

    /**
     * 关联手机号
     */
    @ApiModelProperty(value = "关联手机号")
    private String phone;

    private static final long serialVersionUID = 1L;

    private List<MerchantPermission> merchantPermissions;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> auths = new ArrayList<>();
        List<MerchantPermission> permissions = this.getMerchantPermissions();
        if(permissions != null){
            for (MerchantPermission permission : permissions) {
                auths.add(new SimpleGrantedAuthority(permission.getPermission()));
            }
        }
        return auths;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public static MerchantBuilder builder() {
        return new MerchantBuilder();
    }
}